from dataclasses import dataclass

"""
统一响应格式
"""
@dataclass
class ApiResponse:
    """
    code = 1 : 请求失败
    code = 0 : 请求成功
    """
    code: int
    data: any
    msg: str

    def to_dict(self):
        return {
            'code': self.code,
            'data': self.data,
            'msg': self.msg
        }

def success(data=None):
    return ApiResponse(
        code=0,
        data=data,
        msg='操作成功'
    ).to_dict()

def error(msg='操作失败'):
    return ApiResponse(
        code=1,
        data=None,
        msg=msg
    ).to_dict()
